# encoding=utf-8
from selenium.webdriver.support.ui import WebDriverWait


class SchoolStationOperation():
    def checkphotonum(self, opr, type):
        """
        检查图片数量和数据库是否一致
        :param opr: 操作人本身self
        :param type: 图片类型，具体看数据库C_SchoolImg表里面
        :return:
        """
        type=str(type)
        dr = opr.driver
        disbtns = dr.find_elements_by_xpath(opr.schoolstation_elegant_disimages_xpath)
        disbtncount = disbtns.__len__()
        sql = "SELECT count(1) FROM dbo.C_SchoolImg t WHERE t.Type = " + type + " AND t.SchoolID = (select schoolid from school where schoolname = '" + opr.lbt_schoolname_text + "')"
        querycount = int(opr.selectlbt(sql))
        opr.assertTrue(querycount == (9 - disbtncount))

    def uploadimgforschoolstation(self, opr, file, type):
        """
        上传图片的公共业务方法
        :param opr: self本身
        :param file: self的文件__file__
        :param type: 图片类型，具体看数据库C_SchoolImg表里面
        :return:
        """
        type = str(type)
        dr = opr.driver
        imgs = dr.find_elements_by_xpath("//img")
        # 未上传过的图片数量--上传按钮
        disbtncount = 0
        # 未上传过的图片--图片列表
        disimglist = []
        for img in imgs:
            if str(img.get_property("src")).__contains__(opr.schoolstation_elegant_tempimgname):
                disimglist.append(disbtncount)
            disbtncount += 1
        sql = "SELECT count(1) FROM dbo.C_SchoolImg t WHERE t.Type = " + type + "  AND t.SchoolID = (select schoolid from school where schoolname = '" + opr.lbt_schoolname_text + "')"
        querycount = int(opr.selectlbt(sql))
        opr.assertTrue(querycount == (9 - disimglist.__len__()))
        # 准备操作的图片位置
        oprnum = 0
        # 如果9个格子都已经传满了，就删掉第三个，重新上传
        if disimglist.__len__() == 0:
            delbtns = dr.find_elements_by_css_selector(opr.schoolstation_elegant_delbtns_css)
            delbtns[2].click()
            oprnum = 2
        else:
            oprnum = disimglist[0]
        i = 1
        while str(imgs[oprnum].get_property("src")).__contains__(opr.schoolstation_elegant_tempimgname):
            upbtn = dr.find_elements_by_xpath(opr.schoolstation_elegant_uploadbtns_xpath)[oprnum]
            upbtn.location_once_scrolled_into_view
            upbtn.click()
            opr.uploadImg(3, file)
            print("尝试了" + str(i))
            i += 1

        """sort为从0开始"""
        sql = "SELECT img FROM dbo.C_SchoolImg t WHERE t.Type = " + type + "  AND t.SchoolID = (select schoolid from school where schoolname = '" + opr.lbt_schoolname_text + "')and sort = " + str(
            oprnum)
        queryimg = opr.selectlbt(sql)
        resourcepath = opr.getDir(file, 3) + "\\Resource\\"
        opr.downloadimg(queryimg, resourcepath, "queryimg")
        score = opr.compare_image(resourcepath + "queryimg.jpg", resourcepath + "beautiful.jpg")
        print("照片相似度为：" + str(score))
        opr.assertTrue(int(score * 100) > 90)
        delbtns = dr.find_elements_by_css_selector(opr.schoolstation_elegant_delbtns_css)
        delbtns[oprnum].click()
        delbtns[oprnum]
        WebDriverWait(dr, 30).until(
            lambda dr: dr.find_elements_by_xpath(opr.schoolstation_elegant_uploadbtns_xpath)[oprnum].is_enabled())
        imgs = dr.find_elements_by_xpath("//img")
        opr.assertTrue(str(imgs[oprnum].get_property("src")).__contains__(opr.schoolstation_elegant_tempimgname))
